//
//  OtherCityViewController.swift
//  EasePods
//
//  Created by 李然 on 2024/5/7.
//

import UIKit
import EarbudsFunctions
import RxDataSources
import RxSwift
import RxCocoa

class OtherCityViewController: ViewController {

    let viewModel = OtherCityViewModel()
    
    override func makeUI() {
        super.makeUI()
        
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    override func bindViewModel() {
        super.bindViewModel()
        
        let dataSource = RxTableViewSectionedReloadDataSource<OtherCitySectionModel> { (_, tableView, _, item) in
            switch item {
            case .auto(let cellModel):
                let cell = tableView.dequeueReusableCell(withIdentifier: DefaultSwitchTableViewCell.reuseIdentifier) as! DefaultSwitchTableViewCell
                cell.bind(viewModel: cellModel)
                cell.titleLabel.text = R.string.localizable.setting_weather_auto_location()
                return cell
            case .location(let cellModel):
                let cell = tableView.dequeueReusableCell(withIdentifier: DefaultTableViewCell.reuseIdentifier) as! DefaultTableViewCell
                cell.bind(viewModel: cellModel)
                cell.titleL.text = R.string.localizable.setting_weather_current_area()
                return cell
            case .city(let cellModel):
                let cell = tableView.dequeueReusableCell(withIdentifier: OtherLocationCityNameCell.reuseIdentifier) as! OtherLocationCityNameCell
                cell.bind(viewModel: cellModel)
                return cell
            }
        }
        
        viewModel.dataSource.bind(to: tableView.rx.items(dataSource: dataSource)).disposed(by: rx.disposeBag)
        
        tableView.rx.itemSelected.subscribe(onNext: { [weak self] indexPath in
            guard let self = self else { return }
            guard indexPath.section > 0 else { return }
            let item = self.viewModel.dataSource.value[indexPath.section].items[indexPath.row]
            switch item {
            case .city(let cellModel):
                Weather.shared.city = cellModel.cityName
                self.navigationController?.popViewController(animated: true)
            default:
                break
            }
        }).disposed(by: rx.disposeBag)
        
        tableView.rx.setDelegate(self).disposed(by: rx.disposeBag)
        
        Weather.shared.cityName.asDriver()
            .map{ R.string.localizable.setting_weather_current_city() + "-" + $0 }
            .drive(self.navigation.item.rx.title).disposed(by: rx.disposeBag)
    }
    
    lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .grouped)
        tableView.backgroundColor = R.color.common_10_bg_page()
        tableView.estimatedRowHeight = 70
        tableView.sectionFooterHeight = 0
        tableView.rowHeight = UITableView.automaticDimension
        tableView.sectionIndexColor = R.color.common_23_text_second()
        tableView.sectionIndexBackgroundColor = .clear
        tableView.separatorStyle = .singleLine
        tableView.separatorColor = R.color.common_6_line()
        tableView.separatorInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
        tableView.register(DefaultSwitchTableViewCell.self, forCellReuseIdentifier: DefaultSwitchTableViewCell.reuseIdentifier)
        tableView.register(DefaultTableViewCell.self, forCellReuseIdentifier: DefaultTableViewCell.reuseIdentifier)
        tableView.register(OtherLocationCityNameCell.self, forCellReuseIdentifier: OtherLocationCityNameCell.reuseIdentifier)

        return tableView
    }()
}

extension OtherCityViewController: UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section != 0 {
            return 33
        }
        return 0
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let sectionView = View(frame: CGRect(x: 0, y: 0, width: view.bounds.size.width, height: 33))
        sectionView.backgroundColor = R.color.common_10_bg_page()
        let titleLabel = Label(frame: CGRect(x: 20, y: 0, width: view.bounds.size.width - 40, height: 33))
        titleLabel.text = viewModel.dataSource.value[section].headerTitle
        titleLabel.textColor = R.color.common_3_text()
        titleLabel.font = Fonts.systemFont(ofSize: 12)
        sectionView.addSubview(titleLabel)
        return sectionView
    }
}
